#import "NeXTMail.h"
#import "StringStorage.h"
#import "EnhancedApp.h"
#import "FileName.h"
#import "support.h"
#include <sys/file.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>

const char rtfHeader[] = "{\\rtf0\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\n";
const char templateAttach[] = "{{\\attachment0 %s\n}\n";
const char tarCmd[] = "tar chf - -C %s * | compress > %s";
const char envelopeAddition[] = "NeXT-Attachment: %s, %d, 1/1, 9999, 0\n";
   

@implementation NeXTMail

-initForMUA: (const char *)anApp  andMTA: (const char *)aMailer
{
   [super initForMUA: anApp andMTA: aMailer];
   tmpDir = [[StringStorage alloc] init:"/tmp/.nextmail"];
   [tmpDir mktemp];
   mkdir([tmpDir stringValue],0700);
   attachFile = [[StringStorage alloc] init: [tmpDir stringValue]];
   [attachFile appendStringValue: ".attach"];
   chdir([tmpDir stringValue]);

   [mailFile setStringValue: "/tmp/.mail"];
   [mailFile mktemp];
   fp = fopen([mailFile stringValue], "a");
   if(!fp)
       NXLogError("Failed to open %s", [mailFile stringValue]);

   return self;
}

- free
{
   int x,cnt;

   fclose(fp);

   unlink([mailFile stringValue]);	     /* unlink the mail file */
   unlink([attachFile stringValue]);	     /* unlink the compressed tar file */

   chdir([tmpDir stringValue]);		     /* clean up the enclosure directory */
   unlink("index.rtf");

   for(x = 0, cnt = [attachments count]; x < cnt; x++)
       unlink([[attachments objectAt: x] basename]);

   chdir([NXApp appDirectory]);
   rmdir([tmpDir stringValue]);		     /* remove it */

   [tmpDir free];
   [attachFile free];
   return [super free];
}
   
- buildBody
{
   id aFile;
   struct stat sbuf;
   int indexFile, cnt, x;
   char c;
   char attachString[3 * FILENAME_MAX], resultName[FILENAME_MAX];

   [super buildBody];

   indexFile = open("index.rtf",O_RDWR|O_CREAT,0600);
   if(indexFile == -1)
   {
      NXLogError("Could not create index.rtf\n");
      return nil;
   }

   write(indexFile, rtfHeader, (size_t)strlen(rtfHeader));
					     /* SUCKS - FIX LATER */

   for(x = 0, cnt = [body strlen]; x < cnt; x++)
   {
      c = *(char*)[body elementAt: x];
      switch(c)
      {
       case '\134':
          write(indexFile,"\134\134",2);
          break; 
       case '\n':
          write(indexFile,"\134\n",2);
          break; 
       default:
          write(indexFile,&c,1);
          break;
       }
   }
   
   for(x = 0, cnt = [attachments count]; x < cnt; x++)
   {
      aFile = [attachments objectAt: x];
      sprintf(attachString, templateAttach, [aFile basename]);
      write(indexFile, attachString, (size_t)strlen(attachString));
      symlink([aFile stringValue],[aFile basename]);
   }
   
   write(indexFile,"}\n",2);
   close(indexFile);
					     /* build compressed tar file */
   sprintf(attachString,tarCmd,[tmpDir stringValue], [attachFile stringValue]);
   system(attachString);

   sprintf(resultName,".tar.%d%s", getpid(), [attachFile basename]);

					     /* update envelope */
   stat([attachFile stringValue],&sbuf);
   sprintf(attachString,envelopeAddition,resultName,sbuf.st_size);
   fwrite(attachString,1,strlen(attachString),fp);
   fwrite("\n",1,1,fp);
   
					     /* uueencode and append */
   uuencode(resultName, [attachFile stringValue], fp, 0666);
   return self;
}

+ (BOOL)supportsAttachments
{
   return YES;
}


@end
